package com.inspur.cloud.dao.dataspace;

import com.inspur.cloud.entity.dataspace.HiveResourceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface HiveResourceDao extends JpaRepository<HiveResourceEntity, Integer>, JpaSpecificationExecutor<HiveResourceEntity> {
    @Query("select h from HiveResourceEntity h where databaseName=:databaseName AND parentId=0 ")
    HiveResourceEntity findByDatabaseName(@Param("databaseName") String databaseName);

    @Query("select h from HiveResourceEntity h where owner=:owner AND parentId=0 ")
    HiveResourceEntity findFirstByOwner(@Param("owner") Integer owner);

    HiveResourceEntity findByTableNameAndParentId(String tableName, Integer parentId);

    HiveResourceEntity findByColumnNameAndParentId(String columnName, Integer parentId);

    List<HiveResourceEntity> findByParentId(Integer parentId);

    @Query("select h.id from HiveResourceEntity h where  parentId=:parentId ")
    List<Integer> findIdByParentId(@Param("parentId") Integer parentId);

    @Query("delete from HiveResourceEntity h where h.parentId=:parentId")
    @Modifying
    Integer deleteByParentId(@Param("parentId") Integer parentId);

    List<HiveResourceEntity> findByLevelAndStatus(Integer level, Integer status);

    List<HiveResourceEntity> findByLevelAndStatusAndOwner(Integer level, Integer status, Integer owner);

    @Query("select count(h) from HiveResourceEntity h where parentId=:id ")
    Integer findSonNum(@Param("id") Integer id);

    @Query("update HiveResourceEntity h  set h.status=0  where h.parentId=:parentId")
    @Modifying
    Integer hideByParentId(@Param("parentId") Integer parentId);
    HiveResourceEntity findHiveResourceEntityById(Integer id);
}
